package com.hans.exercise.sort;

/**
 * 快速排序练习
 * Created by Hans on 2014/4/13.
 */
public class JavaQuickSort {
  static int[] numbers = new int[]{8, 1, 3, 2, 9, 4};

  public static void swap(int i, int j) {
    if (i >= j) return;
    int t = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = t;
  }

  public static void sort(int s, int e) {
    if (s >= e) return;
    int m = (s + e) / 2, i = s, j = e;
    int f = numbers[m];
    while (i <= j) {
      while (numbers[i] < f)
        i++;
      while (numbers[j] > f)
        j--;
      swap(i, j);
      i++;
      j--;
    }
    sort(s, m);
    sort(m + 1, e);
  }

  public static void main(String[] args) {
    sort(0, numbers.length - 1);
    for (Integer number : numbers) {
      System.out.print(number + ",");
    }
  }
}
